package com.api.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.api.common.reqres.Request;
import com.api.common.reqres.Response;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.CommodityAdMapper;
import com.api.mapper.CommodityMapper;
import com.api.mapper.CommoditySizeMapper;
import com.api.model.entity.Commodity;
import com.api.model.entity.CommodityAd;
import com.api.model.entity.CommoditySize;
import com.api.model.request.CommodityDetailRequest;
import com.api.model.request.CommodityListRequest;
import com.api.model.response.CommodityDetailResponse;
import com.api.model.response.CommodityListResponse;
import com.api.service.CommodityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-17
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private CommodityMapper commodityMapper;
	@Autowired
	private CommoditySizeMapper commoditySizeMapper;
	@Autowired
	private CommodityAdMapper commodityAdMapper;

	@Override
	public Response<CommodityListResponse> commodityList(Request<CommodityListRequest> request) {
		// TODO Auto-generated method stub
		CommodityListRequest commodityListRequest = request.getBody();
		// 商品广告栏地址
		LambdaQueryWrapper<Commodity> queryWrapper = new LambdaQueryWrapper<Commodity>();
		queryWrapper.eq(Commodity::getStatus, true).orderByDesc(Commodity::getCreatetime);
		if (StringUtils.isNotBlank(commodityListRequest.getType())) {
			queryWrapper.eq(Commodity::getType, commodityListRequest.getType());
		}
		// 设置返回对象
		CommodityListResponse response = new CommodityListResponse();
		List<Commodity> commodityList = commodityMapper.selectList(queryWrapper);
		response.setCommodityList(commodityList);
		List<CommodityAd> commodityAdList = commodityAdMapper.selectList(null);
		response.setCommodityAdList(commodityAdList);
		return responseComponent.toSuccess(response);
	}

	@Override
	public Response<CommodityDetailResponse> commodityDetail(Request<CommodityDetailRequest> request) {
		// TODO Auto-generated method stub
		CommodityDetailRequest commodityDetailRequest = request.getBody();
		requestComponent.checkBody(commodityDetailRequest, CommodityDetailRequest.class);
		// 获取商品信息
		Commodity commodity = commodityMapper.selectById(commodityDetailRequest.getOid());
		// 获取商品规格
		LambdaQueryWrapper<CommoditySize> queryWrapper = new LambdaQueryWrapper<CommoditySize>();
		queryWrapper.eq(CommoditySize::getCommodityid, commodity.getOid()).eq(CommoditySize::getStatus, true);
		List<CommoditySize> commoditySizeList = commoditySizeMapper.selectList(queryWrapper);
		// 设置返回信息
		CommodityDetailResponse response = new CommodityDetailResponse();
		BeanUtils.copyProperties(commodity, response);
		response.setCommoditySizeList(commoditySizeList);
		return responseComponent.toSuccess(response);
	}

}
